import axios from "axios";
import {getStore, removeStore} from "@/plugins/cache";

const service = axios.create({
    baseURL: '/api',
    // baseURL: 'https://blog.wzhyt.cn',
    timeout: 20000 // 请求超时时间
})

service.interceptors.request.use(config => {
    let token = getStore({
        name: 'Token'
    });
    if (token) {
        config.headers['token'] = token;
    }
    return config
}, (error) => {
    return Promise.reject(error)
})


service.interceptors.response.use(
    response => {
        return response.data
    },
    error => {
        console.log('err', error)
        if (error.response.status == 401) {
            removeStore({
                name: 'Token'
            })
            removeStore({
                name: 'UserData'
            })
        }
        return Promise.reject(error)
    }
)
export default service.request
